#include "ControlButton.hpp"
// #include "CBStyle.hpp"

#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include "utilities.hpp" // for loadStylesheet()
#include <Wt/WContainerWidget>
// #include "MainMenu.hpp"

// using namespace Wt;
// using namespace webpages;
// using namespace components;


ControlButton::ControlButton(Wt::WContainerWidget* parent)
    :
        Wt::WContainerWidget(parent),
        is_small(false)
{
    /*
     * ControlButton is styled externally for now. When it is done, it would be good to move the css
     * inline. Use CBStyle for that purpose.
     */
    //Wt::WApplication::instance()->styleSheet().addRule(new webpages::resources::CBStyle);

    /*
     * Style up.
     */
    utilities::loadStylesheet("/css/controlButton.css");
    setStyleClass("control_button");
    
    // TODO rename to control_button-emblem.png and also, the method doesnt check if file exists.
    // what a lucky day to find this site: http://www.ajaxload.info/
    // I can use it to make loading gifs, with custom colors. Will be needed later.
    status_emblem = new Wt::WImage(Wt::WLink("resources/control-button-loading-emblem.gif") , this);
//     status_emblem->resize("-40%", "-40%"); // why does it need to be resized? Explain.
    status_emblem->resize("32px", "32px"); 
     
    status_emblem->setStyleClass("emblem");

     /*
      * Upon clicked(), shrink the ControlButton and pop up the MainMenu.
      */
//      {
         main_menu = new MainMenu;
	 
// 	 !!!!!!!!!!!!!!!!!!
// 	 main_menu->showWithAnimation(); //test delete now.
//          !!!!!!!!!!!!!!!!!!
         
         clicked().connect(this,                &ControlButton::shrink); // For the animation.
         clicked().connect(main_menu,           &MainMenu::popup); 
// 	 clicked().connect(this,           &ControlButton::personStoppedTouching); //test
         // Expand the ControlButton when the MainMenu is about to hide.
         main_menu->aboutToHide().connect(this, &ControlButton::expand);
//      }
     
     // Trying different events. Looking for timing differences and phone compatibility.    
    // mouseWentDown().connect(this, &ControlButton::onClick);
    // mouseMoved().connect(this, &ControlButton::mousedOff);
    // mouseWentOut().connect(this, &ControlButton::mousedOff);
    touchStarted().connect(this, &ControlButton::personIsTouching);
    touchEnded().connect(this, &ControlButton::personStoppedTouching);
}

void ControlButton::personStoppedTouching()
{
    std::cout << "!!!DEBUG: : Why you stopped touched screen bro?!" << std::endl;
}

//TODO: put this in utilities: (it simply shortens the line of code).
// inline addCSSRule(std::string css_selector, std::string css_string)
// {
//     Wt::WApplication::instance()->styleSheet().addRule(css_selector, css_string);
// }

void ControlButton::personIsTouching()
{
    std::cout << "!!!DEBUG: : Da Screen got touched bro!" << std::endl;
}

// Use keyframes to animate the ControlButton "shrinking"
void ControlButton::shrink()
{
    if (is_small != false)
    {
        
        std::cout << "!!!!!!WARNING: AN EXCEPTION SHOULD BE THROWN HERE. This otherwise seemingly " <<
            "impossible condition has occurred. An exception could be used to reset the state." << std::endl;
    }

    // okay go ahead and stretch the ControlButton.
    addStyleClass("control_button_clicked");
    
    is_small = true;
    
    //animate the main menu 
    main_menu->showWithAnimation();
}

// Only valid when the ControlButton is small.
void ControlButton::expand()
{
    if (is_small != true)
    {
        
        std::cout << "!!!!!!WARNING2: AN EXCEPTION SHOULD BE THROWN HERE. This otherwise seemingly " <<
            "impossible condition has occurred. An exception could be used to reset the state." << std::endl;
    }


    // even if this works, should make a control_button_stretch css entry to only contain
    // needed changes.
    removeStyleClass("control_button_clicked");
    addStyleClass("control_button");
    
    is_small = false;
}



